package com.lzh.healthCode.business.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lzh.healthCode.business.entity.*;
import com.lzh.healthCode.business.service.CjInfoService;
import com.lzh.healthCode.business.service.JgInfoService;
import com.lzh.healthCode.business.service.ZyInfoService;
import com.lzh.healthCode.core.BaseConfig;
import com.lzh.healthCode.utils.Constants;
import com.lzh.healthCode.utils.SM2Util;
import com.lzh.healthCode.utils.UpdAreaEnum;
import com.lzh.healthCode.utils.UpdTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 测试类
 *
 * @author suYanLong
 * @date 2022年07月09日 15:58
 */

@RestController
@RequestMapping("test")
@Slf4j
public class TestController extends BaseConfig {

    @Resource
    JgInfoService jgInfoService;

    @Resource
    ZyInfoService zyInfoService;

    @Resource
    CjInfoService cjInfoService;

    @GetMapping("hello")
    public String helloWorld() {
        String cipherData = "BMoL41VN8fNs9VYlm/AFYTvMNq4+Anz0l3qW6iV2pIyyGyIj4K+OTsy/06iJGXs0TRgrvWVvVO/JJIQrMysQrjmdH4Xup/d2vWOHycwtF+81icNkfFqZh/+J71vmP03RivsdQA9HvJofs9LtvEYrovjwK80BIkzrsUqa2qZQpMOZulllQ/JpzHUCEff66TlHSw==";
        String decrypt = SM2Util.decrypt(healthCodeInfo.getPrivateKey(), cipherData);
        log.info(decrypt);
        return "hello world!";
    }

    @GetMapping("register")
    public String register() {
        // 等级注册接口 【参数】
        JSONObject jsonObject = new JSONObject();
        jsonObject.put(Constants.PARAM_KEY_ID, healthCodeInfo.getOrgCode());
        jsonObject.put(Constants.PARAM_KEY_PUBLICK_KEY, healthCodeInfo.getPublicKey());
        // 请求接口
        ResultBean resultBean = httpRequestConfig.sendPost(healthCodeInfo.getRegisterUrl(), jsonObject.toJSONString());
        log.info("登记注册具体接口结果 实体：{}", resultBean);
        return "register";
    }

    @GetMapping("sampleReport")
    public String sampleReport() {
        // 测试人员信息上传接口 【参数】
        List<CjInfoBean> selectAll = cjInfoService.getSelectAll();
        for (CjInfoBean cjInfoBean : selectAll) {
            commonService.uploadSampleReport(UpdAreaEnum.UPD_AREA_TYPE_1, UpdTypeEnum.UPD_TYPE_1, cjInfoBean);
//            commonService.uploadSampleReport(UpdAreaEnum.UPD_AREA_TYPE_2, UpdTypeEnum.UPD_TYPE_1, cjInfoBean);
        }
        return "sampleReport";
    }

    @GetMapping("transportReport")
    public String transportReport() {
        List<ZyInfoBean> selectAll = zyInfoService.getSelectAll();
        for (ZyInfoBean zyInfoBean : selectAll) {
            commonService.uploadtransportReport(UpdAreaEnum.UPD_AREA_TYPE_1, UpdTypeEnum.UPD_TYPE_1, zyInfoBean);
//            commonService.uploadtransportReport(UpdAreaEnum.UPD_AREA_TYPE_2, UpdTypeEnum.UPD_TYPE_1, zyInfoBean);
        }

        return "transportReport";
    }

    @GetMapping("checkResultReport")
    public String checkResultReport() {
        List<JgInfoBean> selectAll = jgInfoService.getSelectAll();
//        int i = 0;
        for (JgInfoBean jgInfoBean : selectAll) {
            commonService.uploadcheckResultReport(UpdAreaEnum.UPD_AREA_TYPE_1, UpdTypeEnum.UPD_TYPE_1, jgInfoBean);
//            commonService.uploadcheckResultReport(UpdAreaEnum.UPD_AREA_TYPE_2, UpdTypeEnum.UPD_TYPE_1, jgInfoBean);
//            if("1".equals(errorCount)){
//                i++;
//            }

        }
//        jgInfoService.updateErrorCount(i);
//        System.out.println(i);
//      ResultBean
        return "checkResultReport";
    }

    @GetMapping("areaList")
    public String areaList() {
        JSONObject jsonObject = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        jsonArray.add(jsonObject);
        jsonArray.toJSONString();
        ResultBean resultBean = httpRequestConfig.sendGet(healthCodeInfo.getAreaListUrl(), jsonArray.toJSONString());
        return "areaList";
    }

    @GetMapping("checkOrgs")
    public String checkOrgs() {
        JSONObject jsonObject = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        jsonArray.add(jsonObject);
        jsonArray.toJSONString();
        ResultBean resultBean = httpRequestConfig.sendGet(healthCodeInfo.getCheckOrgsUrl(), jsonArray.toJSONString());
        return "checkOrgs";
    }

}
